package kr.couponplay.android.activity.deal.category;

import java.util.List;

import kr.couponplay.android.AppContext;
import kr.couponplay.android.Category;
import kr.couponplay.android.activity.R;
import kr.couponplay.android.activity.ResourceUtils;
import kr.couponplay.android.support.DisplayUtils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CategoryExpandableAdapter extends BaseExpandableListAdapter {
	private List<Category> groups;
	private Context context;

	public CategoryExpandableAdapter(Context context, List<Category> groups) {
		this.context = context;
		this.groups = groups;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	
	@Override
	public Category getChild(int groupPosition, int childPosition) {
		return groups.get(groupPosition).getChildren().get(childPosition);
//		return children.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		Category child = getChild(groupPosition, childPosition);

		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.category_child, null);
		}

		TextView childtxt = (TextView) convertView.findViewById(R.id.category_child_TextView);
		childtxt.setText(child.getName());
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return groups.get(groupPosition).getChildren().size();
	}

	@Override
	public Category getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return groups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		Category group = (Category) getGroup(groupPosition);

		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.category_group, null);
		}
		
		ImageView iv = (ImageView) convertView.findViewById(R.id.category_group_icon);
		if(isExpanded){
			iv.setImageResource(R.drawable.arrow_up_16);
		} else {
			iv.setImageResource(R.drawable.arrow_down_16);
		}

		Category mainCategory = AppContext.getMainCategories(convertView.getContext()).get(groupPosition);
		TextView grouptxt = (TextView) convertView.findViewById(R.id.catetory_group_TextView);
		grouptxt.setText(group.getName());
		grouptxt.setCompoundDrawablesWithIntrinsicBounds(ResourceUtils.getDrawable(mainCategory.getCode()), 0, 0, 0);
		grouptxt.setCompoundDrawablePadding(DisplayUtils.pixelFromDp(convertView.getContext(), 10));
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}
}